<?php

defined('BASEPATH') or die('No direct script access allowed');

/**
 * 私信控制器
 * @author  Karson
 * @name    note.php
 * @since   2012-12-2 23:37:51
 */
class Note_Controller extends Controller {
    
    function _init() {
        check_level();
    }
    
    function index($category_id = 0, $page = 1, $pagesize = 10) {
        $where = array();
        if ($category_id > 0)
            $where[] = "n.category_id='{$category_id}'";
        $where[] = "n.user_id='{$this->auth->user_id}'";
        $where = implode(' AND ', $where);
        $result = $this->db->one("SELECT COUNT(*) AS num FROM {pre}note n WHERE {$where}");
        $total = $result['num'];
        $offset = ($page - 1) * $pagesize;
        $pagelist = $this->page->get("/note/index/{$category_id}/{page}/{$pagesize}/", $total, $page, $pagesize, false, false);

        $list = array();
        $this->db->query("SELECT n.*,u.username,u.nickname,u.face FROM {pre}note n 
            LEFT JOIN {pre}user u ON n.from_id=u.id WHERE {$where} ORDER BY createtime DESC LIMIT {$offset},{$pagesize}", "list");
        while ($row = $this->db->get_array("list")) {
            $list[] = $row;
        }
        $data = array("pagelist" => $pagelist, 'list' => $list, 'category_id' => $category_id);
        $this->view->assign($data);
        render("note/index");
    }

    function add() {
        if ($this->form->is_submit()) {
            $username = $this->input->post('username');
            $one = $this->db->one("SELECT * FROM {pre}user WHERE username='{$username}'");
            if ($one) {
                if ($one['id'] != $this->auth->user_id) {
                    $value = array(
                        'user_id' => $one['id'],
                        'from_id' => $this->auth->user_id,
                        'type' => 'private',
                        'content' => $this->input->post('content'),
                        'createtime' => TIME,
                        'state' => 0
                    );
                    $id = $this->db->insert("{pre}note", $value);
                    $info = '';
                    $status = 'y';
                } else {
                    $info = '不能给自己发送私信';
                    $status = 'n';
                }
            } else {
                $info = '未找到接收的玩家';
                $status = 'n';
            }
            dialog_output($status, $info);
        }
        render("note/add");
    }

    function edit($id = 0) {
        $show = $this->db->one("SELECT * FROM {pre}note WHERE id='{$id}' AND user_id='{$this->auth->user_id}'");
        if ($this->form->is_submit()) {
            $notinfamily = $this->input->post('notinfamily');
            $star = $this->input->post('star');
            $value = array(
                'category_id' => $this->input->post('category_id'),
                'title' => $this->input->post('title'),
                'content' => $this->input->post('content'),
                'star' => $star ? 1 : 0,
                'family' => $notinfamily ? 0 : 1,
                'modifytime' => TIME
            );
            $id = $this->db->update("{pre}note", $value, array('id' => $id));
            $result = array('info' => '提交成功！', 'status' => 'y', 'data' => array());
            echo json_encode($result);
            exit;
        }
        $this->view->assign("show", $show);
        render("note/edit");
    }

    function show($id = 0) {
        $show = $this->db->one("SELECT ud.*,u.nickname,u.face FROM {pre}note ud LEFT JOIN {pre}user u ON ud.user_id=u.id WHERE ud.id='{$id}'");
        if ($show && check_in_family($show['user_id'])) {
            $show['isauthor'] = $show['user_id'] == $this->auth->user_id ? 1 : 0;
            $this->view->assign("show", $show);
            render("note/show");
        } else {
            render("error", '参数不正确或授权错误');
        }
    }

    function star($id = 0) {
        $show = $this->db->one("SELECT ud.*,u.nickname,u.face FROM {pre}note ud LEFT JOIN {pre}user u ON ud.user_id=u.id WHERE ud.id='{$id}' AND user_id='{$this->auth->user_id}'");
        if ($show) {
            $outstate = $show['star'] ? 0 : 1;
            $this->db->update("{pre}note", array('star' => $outstate, 'modifytime' => TIME), array('id' => $id));
            json_output(0, $outstate);
        } else {
            json_output(-1, '参数不正确');
        }
    }

    function del($id = 0) {
        $result = $this->db->delete("{pre}note", array("id" => $id, "user_id" => $this->auth->user_id));
        if ($result) {
            json_output(0);
        } else {
            json_output(-1);
        }
    }

}